package com.kaibes.web.api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kaibes.core.spring.bean.BeanUtils;
import com.kaibes.web.response.ResponseFactory;

public class ResponseApi<E, S extends ServiceImpl<?, E>> {

	@Autowired
    protected S service;
	
	@SuppressWarnings("unchecked")
	protected final E createEntity(Object source) {
		E data;
    	if (source.getClass() == service.getEntityClass()) {
			data = (E) source;
		} else {
			data = BeanUtils.createTarget(source, service.getEntityClass());
		}
		return data;
	}
	@SuppressWarnings("unchecked")
    protected final <T> List<E> createEntityList(List<T> source) {
        if (source.isEmpty()) {
            return new ArrayList<E>();
        }
        List<E> dataList;
        if (source.get(0).getClass() != service.getEntityClass()) {
            dataList = new ArrayList<>();
            for (T t : source) {
                E tmp = createEntity(t);
                dataList.add(tmp);
            }
        } else {
            dataList = (List<E>) source;
        }
        return dataList;
    }
	
	// ============POST
	protected final ResponseEntity<E> post(Object source, String successMessage, String failureMessage) {
	    E data = createEntity(source);
    	boolean flag = service.save(data);
    	if (flag) {
			return ResponseFactory.postSuccess(data, successMessage);
		} else {
			return ResponseFactory.postFailure(failureMessage);
		}
    }
    protected final ResponseEntity<List<?>> postList(List<?> source, String successMessage, String failureMessage) {
        List<E> dataList = createEntityList(source);
    	boolean flag = service.saveBatch(dataList);
    	if (flag) {
			return ResponseFactory.postSuccess(dataList, successMessage);
		} else {
			return ResponseFactory.postFailure(failureMessage);
		}
    }
    
    // ============DELETE
    protected final ResponseEntity<Object> deleteById(Number id, String successMessage, String failureMessage) {
        return ResponseFactory.delete(service.removeById(id), successMessage, failureMessage);
    }
    protected final ResponseEntity<Object> deleteById(List<? extends Number> ids, String successMessage, String failureMessage) {
        return ResponseFactory.delete(service.removeByIds(ids), successMessage, failureMessage);
    }
    
    // ============PUT
    protected final ResponseEntity<Object> put(Object source, String successMessage, String failureMessage) {
    	E data = createEntity(source);
        return ResponseFactory.put(service.updateById(data), successMessage, failureMessage);
    }
    
    // ============GET
    protected final ResponseEntity<E> getById(Number id, String successMessage, String failureMessage) {
        return ResponseFactory.get(service.getById(id), successMessage, failureMessage);
    }
    protected final ResponseEntity<List<E>> getById(List<? extends Number> ids, String successMessage, String failureMessage) {
        return ResponseFactory.get(service.listByIds(ids), successMessage, failureMessage);
    }
    
}
